import React from 'react';
import Utils from './../../utils/utils'
import {Table} from 'antd';

export default class ETable extends React.Component{
    //判断是单选还是复选rowSelection会配合change时间使用
    onRowClick = (record,index) => {
        let rowSelection = this.props.rowSelection;
        if(rowSelection == "checkbox") {
            let selectedRowKeys = this.props.selectedRowKeys;
            let selectedItem = this.props.selectedItem;
            let selectedIds = this.props.selectedIds;
            if(selectedIds) {
                const i = selectedIds.indexOf(record.id);
                if(i == -1) {
                    selectedIds.push(record.id);
                selectedRowKeys.push(index);
                selectedItem.push(record)
                } else {
                    selectedIds.splice(i,1);
                    selectedRowKeys.splice(i,1);
                    selectedItem.splice(i,1)
                }
            } else {
                selectedIds = [record.id];
                selectedRowKeys = [index];
                selectedItem = [record]
            }
            this.props.updateSelectedItem(selectedRowKeys,selectedItem,selectedIds)
        } else {
            let selectedRowKeys = [index];
            let selectedItem = record;
            this.props.updateSelectedItem(selectedRowKeys,selectedItem)
        }
    }
    tableInit = () => {
        let row_selection = this.props.rowSelection
        let selectedRowKeys= this.props.selectedRowKeys
 
        const rowSelection = {
            type:"radio",
            //判断点中哪一行
            selectedRowKeys,
            // onChange:this.onSelectChange
        }
        if(row_selection === false || row_selection === null) {
            row_selection = false;
        } else if(row_selection == "checkbox") {
            rowSelection.type = "checkbox"
        } else {
            row_selection = "radio";
        }
        return <Table 
            bordered
            {...this.props}
            rowSelection = {row_selection ? rowSelection : null}
              //配合单选复选使用  她是一个事件  当我们点击某一行的时候就选中了
              onRow={(record,index) => {
                return {
                    onClick:() => {
                        if(!row_selection) {
                            return false
                        }
                        this.onRowClick(record,index)
                    }
                }
            }
              }
        />
    }
    render() {
        return (
            <div>
               { this.tableInit()} 
            </div>
        )
    }
}